libobs_simple\sources\windows\sources/game_capture.rs
1#[cfg(feature = "window-list")]
2use libobs_window_helper::{get_all_windows, WindowInfo, WindowSearchMode};
3use libobs_wrapper::{
4 data::StringEnum,
5 sources::{ObsSourceBuilder, ObsSourceRef},
6};
7
8use super::{ObsHookRate, ObsWindowPriority};
9use crate::define_object_manager;
10
11#[derive(Clone, Copy, Debug, PartialEq, Eq)]
12/// Describes the capture mode of the game capture source.
13pub enum ObsGameCaptureMode {
14 /// Captures any fullscreen application
15 Any,
16 /// Captures a specific window, specified under the `window` property
17 CaptureSpecificWindow,
18 /// CApture the foreground window when a hotkey is pressed
19 CaptureForegroundWindow,
20}
21
22#[derive(Clone, Copy, Debug, PartialEq, Eq)]
23pub enum ObsGameCaptureRgbaSpace {
24 /// sRGB color space
25 SRgb,
26 /// Rec. 2100 (PQ)
27 RGBA2100pq,
28}
29
30impl StringEnum for ObsGameCaptureRgbaSpace {
31 fn to_str(&self) -> &str {
32 match self {
33 ObsGameCaptureRgbaSpace::SRgb => "sRGB",
34 ObsGameCaptureRgbaSpace::RGBA2100pq => "Rec. 2100 (PQ)",
35 }
36 }
37}
38
39impl StringEnum for ObsGameCaptureMode {
40 fn to_str(&self) -> &str {
41 match self {
42 ObsGameCaptureMode::Any => "any_fullscreen",
43 ObsGameCaptureMode::CaptureSpecificWindow => "window",
44 ObsGameCaptureMode::CaptureForegroundWindow => "hotkey",
45 }
46 }
47}
48
49define_object_manager!(
50 #[derive(Debug)]
51 /// A source to capture a game or fullscreen application.
52 ///
53 ///
54 /// Use `GameCaptureSourceBuilder::get_windows` to get a list of windows that can be captured (feature `window-list` needs to be enabled).
55 /// Requires OBS to be running with administrator privileges to capture certain games.
56 ///
57 /// ## Important Notice
58 /// This source fails to capture if another instance (OBS studio, another instance of your program, etc.) has a game capture source for the same game/application active.
59 /// If the window can be captured can be checked using `GameCaptureSourceBuilder::is_window_in_use_by_other_instance` (feature `window-list` needs to be enabled).
60 struct GameCaptureSource("game_capture") for ObsSourceRef {
61 /// Sets the capture mode for the game capture source. Look at doc for `ObsGameCaptureMode`
62 #[obs_property(type_t = "enum_string")]
63 capture_mode: ObsGameCaptureMode,
64
65 /// Sets the window to capture.
66 ///
67 /// # Arguments
68 ///
69 /// * `window` - The window to capture, represented as `ObsString`. Must be in the format of an obs window id
70 ///
71 /// # Returns
72 ///
73 /// The updated `WindowCaptureSourceBuilder` instance.
74 #[obs_property(type_t = "string", settings_key = "window")]
75 window_raw: String,
76
77 #[obs_property(type_t = "enum")]
78 /// Window Match Priority
79 priority: ObsWindowPriority,
80
81 #[obs_property(type_t = "bool")]
82 /// SLI/Crossfire Capture Mode (Slow)
83 sli_compatability: bool,
84
85 #[obs_property(type_t = "bool")]
86 /// Whether the cursor should be captured
87 capture_cursor: bool,
88
89 #[obs_property(type_t = "bool")]
90 /// If transparency of windows should be allowed
91 allow_transparency: bool,
92
93 #[obs_property(type_t = "bool")]
94 /// Premultiplied Alpha
95 premultiplied_alpha: bool,
96
97 /// Limit capture framerate
98 #[obs_property(type_t = "bool")]
99 limit_framerate: bool,
100
101 /// Capture third party overlays (such as steam overlays)
102 #[obs_property(type_t = "bool")]
103 capture_overlays: bool,
104
105 /// Use anti-cheat compatibility hook
106 #[obs_property(type_t = "bool")]
107 anti_cheat_hook: bool,
108
109 /// Hook rate (Ranging from slow to fastest)
110 #[obs_property(type_t = "enum")]
111 hook_rate: ObsHookRate,
112
113 /// The color space to capture in
114 #[obs_property(type_t = "enum_string")]
115 rgb10a2_space: ObsGameCaptureRgbaSpace,
116
117 /// Whether to capture audio from window source (BETA) <br>
118 /// When enabled, creates an "Application Audio Capture" source that automatically updates to the currently captured window/application. <br>
119 /// Note that if Desktop Audio is configured, this could result in doubled audio.
120 capture_audio: bool,
121 }
122);
123
124#[cfg(feature = "window-list")]
125impl GameCaptureSourceBuilder {
126 /// Gets a list of windows that can be captured by this source.
127 pub fn get_windows(mode: WindowSearchMode) -> anyhow::Result<Vec<WindowInfo>> {
128 get_all_windows(mode).map(|e| e.into_iter().filter(|x| x.is_game).collect::<Vec<_>>())
129 }
130
131 /// Checks if a window with the given process ID can be captured by this source.
132 /// This does not guarantee that the window can be captured, only that it is not black
133 pub fn is_window_in_use_by_other_instance(window_pid: u32) -> std::io::Result<bool> {
134 use libobs_window_helper::is_window_in_use_by_other_instance;
135
136 is_window_in_use_by_other_instance(window_pid)
137 }
138
139 /// Sets the window to capture.
140 ///
141 /// # Arguments
142 ///
143 /// * `window` - The window to capture. A list of available windows can be retrieved using `GameCaptureSourceBuilder::get_windows`
144 ///
145 /// # Returns
146 ///
147 /// The updated `GameCaptureSourceBuilder` instance.
148 pub fn set_window(self, window: &WindowInfo) -> Self {
149 self.set_window_raw(window.obs_id.as_str())
150 }
151}
152
153impl ObsSourceBuilder for GameCaptureSourceBuilder {}